import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import jess.Filter;
import jess.JessEvent;
import jess.JessException;
import jess.QueryResult;
import jess.RU;
import jess.Rete;
import jess.Value;
import jess.ValueVector;
import jess.WorkingMemoryMarker;
import jess.JessListener;
import jess.Deftemplate;
import jess.Fact;

/**
 * Class using external JESS expertise in order to decide the 
 * appropriate persons to give blood to a specific person
 *
 */
public class Expert {
	
	/*private static final String COMPATIBILITY = "MAIN::compatibility";
	private static final String WHO = "who";
	private static final String WITH= "with";*/


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Expert().start();
	}
	
	public void start(){
		//create an expert engine
		Rete expertEngine = new Rete();
		//load the expertise
		try {
			expertEngine.batch("House.clp");
			expertEngine.setEventMask(expertEngine.getEventMask() | JessEvent.FACT);
			expertEngine.reset();
			expertEngine.run();
		} catch (JessException e) {
			System.out.println("Error loading the expertise: " +e.toString());
		}
		try {
			expertEngine.reset();
		} catch (JessException e1) {
			System.out.println("Exception reseting the engine: " + e1.toString());
		}
		Iterator objects = expertEngine.getObjects(new Filter() {
		            public boolean accept(final Object o) {
		                return o instanceof Propozitie;
		            }
		        });
		Iterator<Object> it = objects;
        while (it.hasNext()) {
            System.out.println("Person object in WM: " + ((Propozitie) it.next()).getPersoana());
        }

		
		WorkingMemoryMarker initialStatus = expertEngine.mark();
		
		BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
		while (true){
			try {
				
				expertEngine.resetToMark(initialStatus);
				//read the person data
				String name = inputReader.readLine();	
				expertEngine.run();		
			} catch (IOException e) {
				System.out.println("Exception reading person data: " +e.toString());
			} catch (JessException e) {
				System.out.println("Exception specifying the needing blood person: " +e.toString());
			}
			
		}
		
	}
}